﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.File.Models;
using MiniFox.Platform.File.Services;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.File.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class FolderController : BinaryControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        protected virtual IFolderService CreateComponent(string group)
        {
            var _component = ComponentFactory.GetComponent<IFolderService>(group);
            return _component;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Query([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] string path)
        {
            var component = this.CreateComponent(group);
            var result = component.Query(path);
            return result.CreateResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<FolderObject> Create([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] FolderCreation request)
        {
            var component = this.CreateComponent(group);
            var result = component.Create(request.FolderName, request.Path);
            return result.CreateResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<FolderObject> Rename([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] FolderRename request)
        {
            var component = this.CreateComponent(group);
            var result = component.Rename(request.Path, request.NewName);
            return result.CreateResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public Response Remove([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, 
            [FromBody] string path)
        {
            var component = this.CreateComponent(group);
            component.Remove(path);
            return new();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<FolderObject> Move([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] FolderMove request)
        {
            var component = this.CreateComponent(group);
            var result = component.Move(request.FolderId, request.Path);
            return result.CreateResponse();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="folderId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<ZipFileData> Zip([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromQuery] string folderId)
        {
            var component = this.CreateComponent(group);
            var result = component.Zip(folderId);
            return result.CreateResponse();
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="group"></param>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public Response<FolderObject> Copy([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] FolderCopy request)
        //{
        //    var component = this.CreateComponent(group);
        //    var result = component.Copy(request.FolderId, request.Path);
        //    return result.CreateResponse();
        //}

    }
}
